﻿using System.Data;
using System.Web.Mvc;
using PMSMVC.Models;

namespace PMSMVC.Controllers
{
    [Authorize]
    public class ProductTypeController : Controller
    {
        PMSDBContext db = new PMSDBContext();

        //
        // GET: /ProductType/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /ProductType/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /ProductType/Create

        public ActionResult Create(string projectID)
        {
            TempData.Add("ProjectID", projectID);
            return View();
        } 

        //
        // POST: /ProductType/Create

        [HttpPost]
        public ActionResult Create(ProductType productType)
        {            
            if (ModelState.IsValid)
            {
                db.ProductTypes.Add(productType);
                db.SaveChanges();
                return RedirectToAction("Index", "Project");
            }
            else
            {
                return View(productType);
            }
        }

        //public ActionResult PartialCreate()
        //{
        //    return PartialView();
        //}

        //
        // GET: /ProductType/Edit/5
 
        public ActionResult Edit(string id)
        {
            ProductType pro = db.ProductTypes.Find(id);
            return View(pro);
        }

        //
        // POST: /ProductType/Edit/5

        [HttpPost]
        public ActionResult Edit(ProductType pro)
        {
            if (ModelState.IsValid)
            {
                db.Entry(pro).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Details", "Project", new { id = pro.ProjectID });
            }
            return View(pro);
        }

        //
        // GET: /ProductType/Delete/5
 
        public ActionResult Delete(string id)
        {
            ProductType pro = db.ProductTypes.Find(id);
            return View(pro);
        }

        //
        // POST: /ProductType/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            ProductType pro = db.ProductTypes.Find(id);
            string projectID = pro.ProjectID;
            try
            {
                db.Entry(pro).State = EntityState.Deleted;
                db.SaveChanges();
                return RedirectToAction("Details", "Project", new { id = projectID });
            }
            catch
            {
                return View(pro);
            }
        }
    }
}
